package com.jyuxuan.recruitment.utils;

import com.jyuxuan.recruitment.constant.CodeType;
import org.apache.xmlbeans.impl.xb.ltgfmt.Code;

import java.util.LinkedHashMap;

/**
 * 数据封装
 *
 * @param <T>
 */
public class DataMap<T> extends LinkedHashMap {

    private static final long serialVersionUID = 1L;

    private Integer code;

    private String msg;

    private T data;

    private DataMap() {
    }

    public static <T> DataMap<T> success() {
        DataMap<T> dataMap = new DataMap<T>();
        dataMap.code = CodeType.SUCCESS_STATUS.getCode();
        dataMap.msg = CodeType.SUCCESS_STATUS.getMessage();
        return dataMap;
    }

    public static <T> DataMap<T> success(Object code) {
        DataMap<T> dataMap = new DataMap<T>();
        if (code instanceof CodeType) {
            dataMap.code = ((CodeType) code).getCode();
            dataMap.msg = ((CodeType) code).getMessage();
        } else if (code instanceof Integer) {
            dataMap.code = (Integer) code;
        }
        return dataMap;
    }

    public static <T> DataMap<T> fail() {
        DataMap<T> dataMap = new DataMap<T>();
        dataMap.code = CodeType.FAILED_STATUS.getCode();
        dataMap.msg = CodeType.FAILED_STATUS.getMessage();
        return dataMap;
    }

    public static <T> DataMap<T> fail(T code) {
        DataMap<T> dataMap = new DataMap<T>();
        if (code instanceof CodeType) {
            dataMap.code = ((CodeType) code).getCode();
            dataMap.msg = ((CodeType) code).getMessage();
        } else {
            dataMap.code = (Integer) code;
        }
        return dataMap;
    }

    public DataMap message(Object message) {
        if (message instanceof CodeType) {
            this.msg = ((CodeType) message).getMessage();
        } else {
            this.msg = (String) message;
        }
        return this;
    }

    public T getData() {
        return data;
    }

    public DataMap<T> setData(T data) {
        this.data = data;
        return this;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

}
